# -*- coding: utf-8 -*-
import scrapy
import re

from myproject.items import PornhubItem

from scrapy.http import Request


class PhpxyzSpider(scrapy.Spider):
    name = 'phpxyz'
    allowed_domains = ['www.pornhub.com']
    start_urls = ['https://www.pornhub.com/video?o=ht&cc=jp']
    headers = {
        "Host": "www.pornhub.com",
        "Accept": "*/*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "referer": "https://www.pornhub.com/",
        "Connection": "keep-alive",
        "user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
    }

    def parse(self, response):
        # 解析列表页中的所有详情url并交给scrapy进行解析
        host_url = "https://www.pornhub.com"
        last_next_url = '1'
        lis = response.css('.search-video-thumbs li.videoBox')
        for li in lis:
            link =  host_url+li.css('.title a::attr(href)').extract_first()
            yield Request(url=link,callback=self.parse_detail)
            # Request里面可以加一个meta参数，里面放数据传递到回调方法里面
        # 提取下一页并交给scrapy进行下载
        next_url = response.css('.page_next  a::attr(href)').extract_first()
        if next_url:
           last_next_url = host_url+next_url
           if last_next_url:
             yield Request(url=last_next_url,callback=self.parse)
    def parse_detail(self,response):
        str = response.css('#player script::text').extract_first()
        regexg_str = "/iframe>"
        regexg_src="src"
        regexg_frameborder="frameborder"
        regexg_str1 = ".*(src=.*frameborder).*"
        match_str = re.split(regexg_str, str)
        match_str1 = re.split(regexg_src,match_str[0])
        match_str2 = re.split(regexg_frameborder,match_str1[1])
        reg = ".*(\".*\").*"
        last_url = (re.match(reg,match_str2[0])).group(0)
        _https =  last_url[3:-2]

        title = response.css('.inlineFree::text').extract_first()
        support = response.css('.rightCol .votes-count-container .percent::text').extract_first()
        views = response.css('.video-actions-container .rating-info-container .views .count::text').extract()

        view_2= ''.join(views)
        view_3=  re.sub('\D','',view_2)

        print("view_2" +view_2)
        item = PornhubItem()
        item['title'] = title
        item['support'] = support
        item['mp4_url'] = _https
        item['views'] = view_3
        item["html_url"] = response.url
        yield item
